import 'package:flutter/material.dart';
import 'package:flutter_business_2003a/pages/cart/cart_count.dart';
import 'package:flutter_business_2003a/provider/cart_provider.dart';
import 'package:flutter_screenutil/screenutil.dart';
import 'package:provider/provider.dart';
import '../../bean/cart_info_bean_result.dart';

class CartItem extends StatefulWidget {
  final CartInfoBeanResult item;

  const CartItem({Key? key, required this.item}) : super(key: key);

  @override
  State<CartItem> createState() => _CartItemState();
}

class _CartItemState extends State<CartItem> {
  //多选按钮
  Widget _cartCheckBox(CartInfoBeanResult item,context) {
    return Checkbox(
      value: item.isCheck,
      onChanged: (val) {
        item.isCheck = val;
        Provider.of<CartProvider>(context,listen: false).changeCheckState(item);
        setState(() {

        });
      },
      activeColor: Colors.pink,
    );
  }

  //商品图片
  Widget _cartImage(CartInfoBeanResult item) {
    return Container(
      width: ScreenUtil().setWidth(150).toDouble(),
      padding: const EdgeInsets.all(3.0),
      decoration:
          BoxDecoration(border: Border.all(width: 1.0, color: Colors.black12)),
      child: Image.network(item.imagesUrl!),
    );
  }

  //商品名称
  Widget _cartGoodsName(CartInfoBeanResult item) {
    return Container(
        width: ScreenUtil().setWidth(300).toDouble(),
        padding: const EdgeInsets.all(10),
        alignment: Alignment.topLeft,
        child: Column(
          children: [
            Text(
              item.goodsName!,
              maxLines: 2,
              style: TextStyle(fontSize: ScreenUtil().setSp(20).toDouble()),
            ),
            CartCount(item: item)
          ],
        ));
  }

  //商品价格
  Widget _cartPrice(CartInfoBeanResult item) {
    return Container(
      width: ScreenUtil().setWidth(150).toDouble(),
      alignment: Alignment.centerRight,
      child: Column(
        children: [
          Text('¥${item.price}元',
              style: TextStyle(fontSize: ScreenUtil().setSp(20).toDouble())),
          InkWell(
            onTap: () {
              Provider.of<CartProvider>(context,listen: false).deleteOneGood(item.goodsId!);
            },
            child: const Icon(
              Icons.delete_forever,
              color: Colors.black26,
              size: 25,
            ),
          )
        ],
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.fromLTRB(5.0, 2.0, 5.0, 2.0),
      padding: const EdgeInsets.fromLTRB(5.0, 10.0, 5.0, 10.0),
      decoration: const BoxDecoration(
          color: Colors.white,
          border:
              Border(bottom: BorderSide(width: 1.0, color: Colors.black12))),
      child: Row(
        children: [
          _cartCheckBox(widget.item,context),
          _cartImage(widget.item),
          _cartGoodsName(widget.item),
          _cartPrice(widget.item),
        ],
      ),
    );
  }
}
